package com.apobates.forum.toro.template.strategy;

import com.apobates.forum.toro.template.TemplateAffixStatements;
import com.apobates.forum.toro.utils.BeanHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.function.Function;

public class TemplateSerialBasisAffixStrategy<E,T> extends AbstractTemplateAffixStrategy<E> implements TemplateBasisAffixStrategy<E,T>{
    private final static Logger logger = LoggerFactory.getLogger(TemplateSerialBasisAffixStrategy.class);
    @Override
    public Optional<E> apply(
            Class<E> targetObjClass,
            T selectObj,
            List<TemplateAffixStatements> statements,
            Map<String, Function<String, ?>> queries) {
        if(null == targetObjClass || null == selectObj) {
            return Optional.empty();
        }
        E targetObj = BeanHelper.buildProxyInstance(targetObjClass, selectObj);
        // 1: 依次遍历TemplateAffixStatements
        for(TemplateAffixStatements tas : statements){
            /* 2: 前缀affix
            final String affix = tas.getAffix();
            // 3: 当前要设置的属性集合
            Set<String> setAffixAttrs = getAffixAttrSet(targetObjClass, affix);
            // 4: 当前需要设置的属性名和值Map
            Object queryArgVal = BeanHelper.getAttrVal(selectObj, tas.getJoinAttr());
            Map<String,Object> setAttrVals = getGroupObjAttrVal(
                    queryArgVal,
                    queries.get(tas.getNames()),
                    affix,
                    setAffixAttrs);
            // 5: 设置对象的指定属性值集合
            setTargetMultiAttrVal(targetObj, setAttrVals);*/
            bindBasisMultiAttr(tas, targetObjClass, targetObj, selectObj, queries.get(tas.getNames()));
        }
        return Optional.ofNullable(targetObj);
    }

    @Override
    protected String getSentinel() {
        return "Basis";
    }
}